<?php

namespace app\Services\admin;

use app\models\User;
use yii;
use app\commands\Helper;
use app\commands\ErrorMsg;
use app\models\Seat;
use app\models\Seattimeline;
use app\models\Room;
use yii\db\Expression;
use app\Services\ApiService;
use function Couchbase\defaultDecoder;


class SeatService extends ApiService
{

    /**
     * @return Service_Pay
     */
    public static function getInstance()
    {
        return parent::getInstance();
    }

    /**
     * admin
     * 获取会场管理列表
     */
    public function getVenueList($roomID)
    {
        # seat 表 当前
        $seat_list = Seat::find()
            ->where(['roomID' => $roomID])
            ->select('line,seatdate,updatetime')
            ->asArray()
            ->all();
        $update_time = $seat_list[0]['updatetime'];

        # 大于updatetime 时间之后的数据
        # 1=进入\n2=离开\n3=踢出\n4=在房间内\n5=坐下\n6=未选座的情况下离开
        $back_list = Seattimeline::find()
            ->where(['roomID' => $roomID])
            ->andWhere(['>=', 'updatetime', $update_time])
            ->andWhere(['in', 'status', [1, 2, 4, 3, 5]])
            ->select('row,col,status,userID,updatetime')
            ->asArray()
            ->all();

        if (!empty($back_list)) {
            foreach ($back_list as $info) {
                if (($info['row'] - 1)>=0){
                    # status 为 1和5 确定选好座位   2和3  为离开  位子是0
                    if (!empty($info['status']) && ($info['status'] == 1 ||  $info['status'] == 4 || $info['status'] == 5) && !empty($info['row']) && !empty($info['col'])) {
                        $row =  intval($info['row']) - 1;
                        $col =  intval($info['col']) - 1;
                        $seat_info = explode('/', $seat_list[$info['row'] - 1]['seatdate']);
                        $seat_info[$col] = $info['userID'];
                    } else {
                        $row =  intval($info['row']) - 1;
                        $col =  intval($info['col']) - 1;
                        $seat_info = explode('/', $seat_list[$info['row'] - 1]['seatdate']);
                        $seat_info[$col] = 0;
                    }
                }
            }
            if (($info['row'] - 1)>=0){
                $seat_str = implode('/', $seat_info);
                $seat_list[$info['row'] - 1]['seatdate'] = $seat_str;
            }
        }
        $seat_user_list = [];
        $letter_arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"];
        foreach ($seat_list as &$row) {
            $seat_arr = explode('/', $row['seatdate']);
            $seat_list_arr = [];
            foreach ($seat_arr as $key => &$user) {
                $seat_list_arr[$key] = [];
                if (!empty($user) && intval($user) > 0) {
                    $user_info = User::find()
                        ->where(['ID' => $user])
                        ->select('ID,Company,Position,Name')
                        ->asArray()->one();
                    $user_info['seat_num'] = $letter_arr[intval($row['line'] - 1)] . intval($key + 1);
                    $seat_list_arr[$key] = $user_info;
                }
            }
            $row = $seat_list_arr;
        }
        return ['code' => 200, 'data' => $seat_list];
    }

    /**
     * 用户踢出会场
     */
    public function kickUser($userID, $roomID, $seat_num)
    {
        # 检查当前人数
        $room_info = Room::find()
            ->select('curseat')
            ->where(['ID' => $roomID])
            ->asArray()
            ->one();
        if ($room_info['curseat'] > 0) {
            # 会场人数-1
            $res = Yii::$app->db->createCommand()->update('`pltf.room`', [
                'curseat' => new Expression('curseat-1'),
            ], ['ID' => $roomID])->execute();
        }
        # Seattimeline 新增一条数据 踢人数据
        $time = date("Y-m-d H:i:s", time());
        $seat_num_row = substr($seat_num, 0, 1);
        $letter_arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
        $seat_num_row = array_search($seat_num_row, $letter_arr);
        $seat_num_row += 1;
        $seat_num_col = substr($seat_num, 1);
        $insert = [
            'roomID' => $roomID,
            'row'    => $seat_num_row,
            'col'    => $seat_num_col,
            'userID' => $userID,
            'status' => 3,
            'updatetime' => $time
        ];
        $res = Yii::$app->db->createCommand()->insert('`pltf.seattimeline`', $insert)->execute();

        # 更新seat 表
        $info  = Seat::find()
            ->select('seatdate')
            ->where(['roomID' => $roomID,'line'=>$seat_num_row])
            ->asArray()
            ->one();
        $seatdate_arr = explode('/',$info['seatdate']);
        $seatdate_arr[(int)$seat_num_col-(int)1] = 0;
        $seatdate = implode('/',$seatdate_arr);
        $update = [
            'seatdate'       => $seatdate,
            'updatetime' => $time
        ];
        $res = Yii::$app->db->createCommand()->update('`pltf.seat`', $update,['roomID'=>$roomID])->execute();
        if ($res) {
            return ['code' => 200, 'message' => '操作成功'];
        } else {
            return ['code' => -1, 'message' => '操作失败'];
        }
    }


}
